home *** CD-ROM | disk | FTP | other *** search
/ CDUTIL 13 / CDUTIL #13 Julio 1995.iso / windows / acadcom / ads / sample / appmngr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-08  |  12.8 KB  |  375 lines

  1. /* Next available MSG number is  33 */
  2.  
  3. /***************************************************************************
  4.    Module Name:  appmngr.c
  5.  
  6.    Copyright (C) 1992, 1993, 1994 by Autodesk, Inc.
  7.  
  8.    Permission to use, copy, modify, and distribute this software in 
  9.    object code form for any purpose and without fee is hereby granted, 
  10.    provided that the above copyright notice appears in all copies and 
  11.    that both that copyright notice and the limited warranty and 
  12.    restricted rights notice below appear in all supporting 
  13.    documentation.
  14.  
  15.    AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.  
  16.    AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF 
  17.    MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE.  AUTODESK, INC.
  18.    DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE 
  19.    UNINTERRUPTED OR ERROR FREE.
  20.  
  21.    Use, duplication, or disclosure by the U.S. Government is subject to 
  22.    restrictions set forth in FAR 52.227-19 (Commercial Computer 
  23.    Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii) 
  24.    (Rights in Technical Data and Computer Software), as applicable.
  25.     
  26.    .
  27.  
  28.    Description:  App loads and unloads a series of ADS apps, and displays
  29.                  The currently loaded ADS applications.
  30.  
  31.    Author     :
  32.                  Autodesk, Inc.
  33.                  2320 Marinship Way
  34.                  Sausalito, CA. 94965
  35.                  (415)332-2344
  36.  
  37.    Function Entry Points:
  38.         void
  39.         main(argc, argv)
  40.         int    argc;              [Number of argument passed in cmd line]
  41.         char   *argv[];           [Array of pointers to arguments]
  42.  
  43.    Exported ADS Functions
  44.         TESTXLOAD                 [Test ADS_ XLOAD/XUNLOAD/LOADED functions]
  45.  
  46.    Modification History:
  47.         Feb 26 1992 - bcm - original creation
  48.  
  49.    Notes and restrictions on use:
  50.  
  51.  
  52. ***************************************************************************/
  53.  
  54. /**************************************************************************/
  55. /*  MODULE NAME  */
  56. /**************************************************************************/
  57. #define    APPMNGR
  58.  
  59. /****************************************************************************/
  60. /*  DEFINES  */
  61. /****************************************************************************/
  62. #define ELEMENTS(array) (sizeof(array)/sizeof((array)[0]))
  63.  
  64. /**************************************************************************/
  65. /*  TYPEDEFS  */
  66. /**************************************************************************/
  67. /* ADS Function Table */
  68. typedef struct {
  69.     char    *name;
  70.     int     (*fptr)();
  71. } ftblent;
  72.  
  73. typedef struct resbuf rbtype;
  74.  
  75. /**************************************************************************/
  76. /*  INCLUDES  */
  77. /**************************************************************************/
  78.  
  79. #include <stdio.h>
  80. #include "adslib.h"
  81. #include "ol_errno.h"
  82. /****************************************************************************/
  83. /*  LOCAL FUNCTION FORWARD DECLARATIONS  */
  84. /****************************************************************************/
  85. int     testappmngr();
  86.  
  87. /**************************************************************************/
  88. /*  GLOBAL VARIABLES  */
  89. /**************************************************************************/
  90. /* Table of ADS functions */
  91. ftblent exfun[] = {
  92.             {/*MSG0*/"C:TESTAPPMNGR", testappmngr},
  93.         };
  94.  
  95. /* Table of ADS applications */
  96. char    *AppTable[] = {
  97.             /*MSG0*/"ads_perr",
  98.             /*MSG0*/"appmngr",
  99.             /*MSG0*/"arbmat",
  100.             /*MSG0*/"asctext",
  101.             /*MSG0*/"colext",
  102.             /*MSG0*/"dlgtest",
  103.             /*MSG0*/"dragger",
  104.             /*MSG0*/"fact",
  105.             /*MSG0*/"gpalsym",
  106.             /*MSG0*/"gravity",
  107.             /*MSG0*/"magnets",
  108.             /*MSG0*/"project",
  109.             /*MSG0*/"ptext",
  110.             /*MSG0*/"sld2ps",
  111.             /*MSG0*/"sqr",
  112.             /*MSG0*/"tower",
  113.         };
  114.  
  115. /**************************************************************************/
  116. /*  EXTERNAL FUNCTION DECLARATIONS  */
  117. /**************************************************************************/
  118.  
  119. /**************************************************************************/
  120. /*  EXTERNAL VARIABLE DECLARATIONS  */
  121. /**************************************************************************/
  122.  
  123. /****************************************************************************/
  124. /*  LOCAL FUNCTION DECLARATIONS  */
  125. /****************************************************************************/
  126.  
  127. int funcload   _((void));
  128. int funcunload _((void));
  129. int dofun      _((void));
  130. int geterrno   _((void));
  131.  
  132. /******************************************************************************/
  133. /*.doc geterrno(internal) */
  134. /*+
  135.     This function is called to obtain the value of the AutoCAD system
  136.     variable ERRNO and return it as a result.
  137. -*/
  138. /******************************************************************************/
  139. int
  140. /*FCN*/geterrno()
  141. {
  142.     rbtype errval;
  143.  
  144.     ads_getvar(/*MSG0*/"ERRNO", &errval);
  145.  
  146.     return errval.resval.rint;
  147. }
  148.  
  149. /******************************************************************************/
  150. /*.doc funcload(internal) */
  151. /*+
  152.     This function is called to define all function names in the ADS
  153.     function table.  Each named function will be callable from lisp or
  154.     invokable from another ADS application.
  155. -*/
  156. /******************************************************************************/
  157. int
  158. /*FCN*/funcload()
  159. {
  160.     int i;
  161.  
  162.     for (i = 0; i < ELEMENTS(exfun); i++) {
  163.         if (!ads_defun(exfun[i].name, i))
  164.             return RTERROR;
  165.     }
  166.  
  167.     return RTNORM;
  168. }
  169.  
  170. /******************************************************************************/
  171. /*.doc funclunoad(internal) */
  172. /*+
  173.     This function is called to undefine all function names in the ADS
  174.     function table.  Each named function will be removed from the
  175.     AutoLISP hash table.
  176. -*/
  177. /******************************************************************************/
  178. int
  179. /*FCN*/funcunload()
  180. {
  181.     int i;
  182.  
  183.     /* Undefine each function we defined */
  184.  
  185.     for (i = 0; i < ELEMENTS(exfun); i++) {
  186.         ads_undef(exfun[i].name,i);
  187.     }
  188.  
  189.     return RTNORM;
  190. }
  191. /******************************************************************************/
  192. /*.doc dofun(internal) */
  193. /*+
  194.     This function is called to invoke the function which has the
  195.     registerd function code that is obtained from  ads_getfuncode.  The
  196.     function will return RTERROR if the function code is invalid, or
  197.     RSERR if the invoked function fails to return RTNORM.  The value
  198.     RSRSLT will be returned if the function code is valid and the
  199.     invoked subroutine returns RTNORM.
  200. -*/
  201. /******************************************************************************/
  202. int
  203. /*FCN*/dofun()
  204. {
  205.     int    val;
  206.     int    rc;
  207.  
  208.     ads_retvoid();
  209.         
  210.     if ((val = ads_getfuncode()) < 0 || val > ELEMENTS(exfun))
  211.         return RTERROR;
  212.  
  213.     rc = (*exfun[val].fptr)();
  214.  
  215.     return ((rc == RTNORM) ? RSRSLT:RSERR);
  216. }
  217.  
  218. /******************************************************************************/
  219. /*.doc main(internal) */
  220. /*+
  221.     This is the main entry point for the ADS application.  All ADS
  222.     requests will be dispatched from this function.  This is your
  223.     standard ADS dispatch loop.
  224. -*/
  225. /******************************************************************************/
  226. void
  227. /*FCN*/main(argc,argv)
  228. int argc;
  229. char *argv[];
  230. {
  231.     short scode = RSRSLT;          /* Normal result code (default) */
  232.     int   stat;
  233.     char  errmsg[80];
  234.  
  235.     ads_init(argc, argv);          /* Initiate communication with AutoLISP */
  236.  
  237.     for ( ;; ) {                   /* Request/Result loop */
  238.  
  239.         if ((stat = ads_link(scode)) < 0) {
  240.             sprintf(errmsg,
  241.                     /*MSG1*/"APPMNGR: bad status from ads_link() = %d\n",
  242.                     stat);
  243. #ifdef Macintosh
  244.             macalert(errmsg);
  245. #else
  246.             puts(errmsg);
  247.             fflush(stdout);
  248. #endif /* Macintosh */
  249.             exit(1);
  250.         }
  251.  
  252.         scode = RSRSLT;           /* Reset result code */
  253.  
  254.         switch (stat) {
  255.  
  256.         case RQXLOAD:             /* Load & define functions */
  257.             scode = funcload() ? -RSRSLT : -RSERR;
  258.             break;
  259.  
  260.         case RQXUNLD:             /* Unload functions */
  261.             scode = funcunload() ? -RSRSLT : -RSERR;
  262.             ads_printf(/*MSG2*/"Unloading.\n");
  263.             break;
  264.  
  265.         case RQSUBR:             /* Handle request for external function */
  266.             dofun();
  267.             break;
  268.  
  269.         default:
  270.             break;
  271.         }
  272.     }
  273. }
  274.  
  275. /******************************************************************************/
  276. /*.doc testappmngr(internal) */
  277. /*+
  278.     This function is called from dofun function as a result of RQSUBR
  279.     request being sent to the main dispatch loop.
  280.  
  281.     The function will walk thru the AppTable and perform an ADS_XLOAD
  282.     on each application.  The message Loading apname... will be printed
  283.     before each application is attempted to be loaded.  If the load
  284.     fails, then the message will be followed by the messaged FAILED..
  285.  
  286.     The next phase of the test will make a call to the function
  287.     ADS_LOADED to obtain the list of currently loaded applications.  It
  288.     will then walk thu the list and perform an ADS_XUNLOAD on each
  289.     application.  The message Unloading appname... will be printed
  290.     before each application is attempted to be unloaded.  Note, that
  291.     some applications may generate messages of their own, so the output
  292.     may not be exactly as described.  If an application fails to be
  293.     unloaded, then the message FAILED will follow the unloading
  294.     message.
  295.  
  296.     This function always returns RTNORM.
  297. -*/
  298. /******************************************************************************/
  299. int
  300. /*FCN*/testappmngr()
  301. {
  302.     rbtype      *args;
  303.     rbtype      *tapl;
  304.     rbtype      *applist;
  305.     int         x;
  306.     int         i;
  307.     int         pcount;
  308.  
  309.     args = ads_getargs();
  310.     ads_printf(/*MSG3*/"\nPerforming ads_xload, ads_xunload, and ads_loaded test\n");
  311.     pcount = 5;
  312.     if (args != NULL) {
  313.         if (args->restype == RTSHORT)
  314.             pcount = args->resval.rint;
  315.     }
  316.  
  317.     for (i = 1; i <= pcount; i++) {
  318.         ads_printf(/*MSG4*/"\n\n\nPass %d.\n\n\n", i);
  319.         for (x = 0; x < ELEMENTS(AppTable); x++) {
  320.             ads_printf(/*MSG5*/"Loading %s...\n", AppTable[x]);
  321.             if (ads_xload(AppTable[x]) != RTNORM)
  322.                 switch(geterrno()) {
  323.                 case OL_ENULLPTR:
  324.                     ads_printf(/*MSG6*/"Filename argument was a NULL pointer.\n");
  325.                     break;
  326.                 case OL_EOPEN:
  327.                     ads_printf(/*MSG7*/"Specified file could not be opened.\n");
  328.                     break;
  329.                 case OL_ELOADED:
  330.                     ads_printf(/*MSG8*/"Specified file is already loaded.\n");
  331.                     break;
  332.                 case OL_ENOMEM:
  333.                     ads_printf(/*MSG9*/"No room for app control block.\n");
  334.                     break;
  335.                 case OL_EMAXAPP:
  336.                     ads_printf(/*MSG10*/"Max number of ADS apps loaded.\n");
  337.                     break;
  338.                 case OL_EEXEC:
  339.                     ads_printf(/*MSG11*/"App could not be executed.\n");
  340.                     break;
  341.                 case OL_EVERSION:
  342.                     ads_printf(/*MSG12*/"App contains wrong ADS version #.\n");
  343.                     break;
  344.                 }
  345.         }
  346.  
  347.         for (tapl = applist = ads_loaded(); tapl != NULL; tapl = tapl->rbnext) {
  348.             if (tapl->restype == RTSTR && tapl->resval.rstring != NULL) {
  349.                 ads_printf(/*MSG13*/"Unloading %s...\n", tapl->resval.rstring);
  350.                 if (ads_xunload(tapl->resval.rstring) != RTNORM)
  351.                     switch(geterrno()) {
  352.                     case OL_ENULLPTR:
  353.                         ads_printf(/*MSG14*/"Filename argument was a NULL pointer.\n");
  354.                         break;
  355.                     case OL_EDENIED:
  356.                         ads_printf(/*MSG15*/"An app cannot unload itself this way,\n");
  357.                         ads_printf(/*MSG16*/"and nested apps cannot be unloaded.\n");
  358.                         break;
  359.                     case OL_EREFUSE:
  360.                         ads_printf(/*MSG17*/"App refused to unload.\n");
  361.                         break;
  362.                     case OL_ENOTLOADED:
  363.                         ads_printf(/*MSG18*/"App is not loaded, so it\n");
  364.                         ads_printf(/*MSG19*/"cannot be unloaded.\n");
  365.                         break;
  366.                     }
  367.             }
  368.         }
  369.  
  370.         if (applist != NULL)
  371.             ads_relrb(applist);
  372.     }
  373.     return RTNORM;
  374. }
  375.